/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

Ext.ns("CRM.SPComp");

(function() {

	CRM.SPComp.createHeader = function(firstActiveTabIndex, secondActiveTabIndex) {
		
		var salesSecondTab = secondActiveTabIndex;
		var marketingSecondTab = secondActiveTabIndex;
		var projectSecondTab = secondActiveTabIndex;
		var adminSecondTab = secondActiveTabIndex;
		
		function redirectPageToSales(absoluteUrl, salesSubTabActiveTabIndex){
			var newPath = absoluteUrl;
			var currentUrl = window.location.href;
			var currentPath = currentUrl.substring(currentUrl.lastIndexOf("/")+1, currentUrl.length);
			if(currentPath != newPath){
				window.location = newPath;
				}
				salesTab.setActiveTab(salesSubTabActiveTabIndex);
				
				
		};	
		
		function redirectPageToMarketing(absoluteUrl, marketingSubTabActiveTabIndex){
			var newPath = absoluteUrl;
			var currentUrl = window.location.href;
			var currentPath = currentUrl.substring(currentUrl.lastIndexOf("/")+1, currentUrl.length);
			if(currentPath != newPath){
				window.location = newPath;
				}
				marketingTab.setActiveTab(marketingSubTabActiveTabIndex);
		};		
		
		function redirectPageToProject(absoluteUrl, projectSubTabActiveTabIndex){
			var newPath = absoluteUrl;
			var currentUrl = window.location.href;
			var currentPath = currentUrl.substring(currentUrl.lastIndexOf("/")+1, currentUrl.length);
			if(currentPath != newPath){
				window.location = newPath;
				}
				projectTab.setActiveTab(projectSubTabActiveTabIndex);
		};	
		
		function redirectPageToAdmin(absoluteUrl, adminSubTabActiveTabIndex){
			var newPath = absoluteUrl;
			var currentUrl = window.location.href;
			var currentPath = currentUrl.substring(currentUrl.lastIndexOf("/")+1, currentUrl.length);
			if(currentPath != newPath){
				window.location = newPath;
				}
				adminTab.setActiveTab(adminSubTabActiveTabIndex);
		};		
		
		var consoleTab = {
			xtype : 'tabpanel',
			id : 'consoleTab',
			title : 'Console',
			items : []
			
		};

		var calendarTab = {
				xtype : 'tabpanel',
				id : 'calendarTab',
				title : 'Calendar',
				items : []
		};
		
		var leadTab = {
			xtype : 'tabpanel',
			id : 'leadTab',
			title : 'Lead',
			items : [],
			listeners : { activate:
							function(){
								redirectPageToSales('lead', secondActiveTabIndex);
							}	
						}
		};
		
		var customerTab = {
				xtype : 'tabpanel',
				id : 'customerTab',
				title : 'Customer',
				items : [],
				listeners : { activate:
								function(){
									redirectPageToSales('customer', secondActiveTabIndex);
								}					
							}
			};
		
		var opportunityTab = {
				xtype : 'tabpanel',
				id : 'opportunityTab',
				title : 'Opportunity',
				items : [],
				listeners : { activate:
								function(){
									redirectPageToSales('opportunity', secondActiveTabIndex);
								}				
							}
			};
		
		var activityTab = {
				xtype : 'tabpanel',
				id : 'activityTab',
				title : 'Activity',
				items : [],
				listeners : { activate: 
								function(){
										redirectPageToSales('activity', secondActiveTabIndex);
									}
							}
			};
		
		var searchTab = {
				xtype : 'tabpanel',
				id : 'searchTab',
				title : 'Search',
				items : [],
				listeners : { activate: 
					function(){
							redirectPageToSales('viewchart', secondActiveTabIndex);
						}
				}
			};
		
		var reportTab = {
				xtype : 'panel',
				id 	  : 'reportTab',
				title : 'Report',
				items : [],
				listeners : { activate: 
							  function(){
									redirectPageToSales('report', secondActiveTabIndex);
								}
							}
			};
		
		
		//end of sales tab sub tabs
		
		var setupTab = {
				xtype : 'panel',
				id : 'setupsid',
				title : 'Setups',
				items : [],
				listeners : { activate:
							  function(){
									redirectPageToSales('setup', secondActiveTabIndex);
								}
							}
			};
		
		//project modules tab
		var projectHomeTab = {
				xtype 	  : 'panel',
				id    	  : 'projectHomeTab',
				title 	  : 'Home',
				items	  : []
		};
		
		var projectInfoTab = {
				xtype 	  : 'panel',
				id    	  : 'projectInfoTab',
				title 	  : 'Project Manager',
				listeners : { activate: 
							  function(){
								redirectPageToProject('project', secondActiveTabIndex);
								}
							  }
		};
		
		var projectTaskTab = {
				xtype 	  : 'panel',
				id    	  : 'projectTaskTab',
				title 	  : 'Task',
				items	  : []
		};
		
		var projectMemberTab = {
				xtype 	  : 'panel',
				id    	  : 'projectMemberTab',
				title 	  : 'Member',
				items	  : []
		};
		
		var projectIssueTab = {
			    xtype    : 'panel',
			    id       : 'projectIssueTab',
			    title    : 'Issues',
			    //items   : []
			    listeners : { activate: 
			       function(){
			      redirectPageToProject('issue', secondActiveTabIndex);
			      }
			    }
		 };
		
		var projectEngagementTab = {
				xtype 	  : 'panel',
				id    	  : 'projectEngagementTab',
				title 	  : 'Engagement',
				items	  : []
		};		
		
		//admin module tabs
		var userManagerPanel = {
				xtype	  : 'panel',
				id		  : 'userManagerTab',
				title	  : 'User Manager',
				items	  : [],
				listeners : { activate: 
							  function(){
								redirectPageToAdmin('userManager', secondActiveTabIndex);
								}
							  }
		};
		
		var groupPanel = {
				xtype	  : 'panel',
				id		  : 'groupPanel',
				title	  : 'Groups',
				items	  : []
		};
		
		var rolePanel = {
				xtype	  : 'panel',
				id		  : 'rolePanel',
				title	  : 'Roles',
				items	  : []
		};
		
		var permissionPanel = {
				xtype	  : 'panel',
				id		  : 'permissionPanel',
				title	  : 'Permissions',
				items	  : [],
				listeners : { activate: 
							  function(){
								redirectPageToAdmin('permission', secondActiveTabIndex);
								}
							 }	
		};
		
		
		//master panels
		var salesTab = new Ext.TabPanel({
			xtype 	  : 'tabpanel',
			title 	  : 'Sales',
			id 		  : 'salesTab',
			//activeTab : secondActiveTabIndex,
			activeTab : salesSecondTab,
			items 	  : [ consoleTab, 
				          calendarTab,
				          leadTab, 
				          customerTab,
				          opportunityTab, 
				          activityTab, 
				          searchTab, 
				          reportTab, 
				          setupTab
				        ],
			listeners	: {activate: function(){
							var currentlyClickedTab = Ext.getCmp('salesTab').getActiveTab().id;
							if(currentlyClickedTab == null){
								Ext.getCmp('salesTab').activate(leadTab.id);
							}else{
								Ext.getCmp('salesTab').activate(currentlyClickedTab);
							}
						  }}
		});

		var marketingTab = new Ext.TabPanel({
			xtype 		: 'tabpanel',
			title 		: 'Marketing',
			id 	  		: 'marketingTab',
			items 		: []
		});		
		
		var projectTab = new Ext.TabPanel({
			xtype		: 'tabpanel',
			title		: 'Project',
			id			: 'projectTab',
			//activeTab	: projectSecondTab,
			items		: [projectHomeTab, projectInfoTab, projectTaskTab, /*projectMemberTab,*/ projectIssueTab, projectEngagementTab],
			listeners	: {activate: function(){
							Ext.getCmp('projectTab').activate(projectInfoTab.id);
						  }}
		});
		
		var adminTab = new Ext.TabPanel({
			xtype		: 'tabpanel',
			title		: 'Administrator',
			id			: 'adminTab',
			//activeTab	: projectSecondTab,
			items		: [userManagerPanel, groupPanel, rolePanel, permissionPanel],
			listeners	: {activate: function(){
								/*
								var currentTopTab = Ext.getCmp('adminTab');
								var defaultTab = userManagerPanel.id;
								if(currentTopTab.getActiveTab() == null || currentTopTab.getActiveTab().id == defaultTab){
									currentTopTab.activate(defaultTab);
								}else{
									var currentlyClickedTab = currentTopTab.getActiveTab().id;
									alert(currentlyClickedTab);
									currentTopTab.activate(currentlyClickedTab);
								}*/
						  }}
		});		

		var headerTab = new Ext.TabPanel({
			width 		: '100%',
			height 		: 200,
			id 			: 'headerTab',
			activeTab 	: firstActiveTabIndex,
			items 		: [salesTab, marketingTab, projectTab, adminTab]
		});
		
		var tbarPanel = {
			xtype		: 'panel',
			id			: 'tbarPanel',
			layout		: 'vbox',
			buttons		: []
		};
		
		var mainHeaderPanel = new Ext.Panel({
			width		: '100%',
			autoHeight	: true,
			id 			: 'mainPanel',
			//title		: 'Logger Details',
			//layout	: 'hbox',
			frame		: true,
			items 		: [
			      		   	headerTab
			      		   ],
            tbar		: {
                		   		xtype	: 'toolbar',
                		   		buttonAlign	: 'right',
                		   		items	: [
                		   		     	   	{
											  	xtype	: 'displayfield',
											  	id		: 'loginDetailsTF',
											  	autoWidth: true
											 },			       
											 '-',
							    			 {
											    xtype	: 'button',
											    text	: 'Logout',
											    width	: 50,
											    handler	: logout
											}
                		   		     	   ]
            				}

		});
		return mainHeaderPanel;
	};
	
	function loginDetails(){
		Ext.Ajax.request({
			url			: 'home/loginDetails',
			method		: 'GET',
			waitMessage : 'Loading...',
			success		: function(result, request){
							var jsonData = Ext.util.JSON.decode(result.responseText);
							//Ext.getCmp('loginDetailsTF').setValue(jsonData.data); 
						 },
			failure		: function(){
							alert("User Details not found.");
						 }
		});
	}
	
	function logout(){
		Ext.Ajax.request({
			url		: 'login/logout',
			method	: 'GET',
			success	: function(){
						 Ext.Msg.alert('Confirm', 'Successfully Logout');
						 window.location = 'login';
						},
			failure	: function(){
							Ext.Msg.alert('Error', 'Failure');
						}			
		});
	}
	
	loginDetails();

})();